<template>
  <div class='login-box'>
    <div class='login-content-box'>
      <el-form :model="FormData" :rules="FormData_rules" ref='Form_ref'>
        <el-form-item prop='username'>
          <el-input v-model='FormData.username' clearable placeholder="请输入账号" prefix-icon="el-icon-user"></el-input>
        </el-form-item>
        <el-form-item prop='password'>
          <el-input v-model='FormData.password' clearable placeholder="请输入密码" type='password' prefix-icon="el-icon-goods"></el-input>
        </el-form-item>
      </el-form>
      <div class='login-button-box'>
        <el-button size='medium' type="primary" @click='login' >登陆</el-button>
        <el-button size='medium' type="success" @click='reset' >重置</el-button>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      FormData:{
        username:'',
        password:''
      },
      FormData_rules:{
        username: [
          { required: true, message: '请输入账号', trigger: 'blur' },
          { min: 5, max: 15, message: '长度在 5 到 15 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入账号', trigger: 'blur' },
          { min: 5, max: 15, message: '长度在 5 到 15 个字符', trigger: 'blur' }
        ],
      }
    }
  },
  methods: {
    async login() { // 登陆函数
      let rel = await this.$refs.Form_ref.validate().catch( err=>err )
      if( rel ) { // 验证成功
        let res = await this.axios.post('login', this.FormData)
        if( res.meta.status === 200 ) {
          window.sessionStorage.setItem('token', res.data.token)
          this.pop.success( res.meta.msg )
          this.$router.push('/Main')
        } else {
          this.pop.error( res.meta.msg )
        }
      }
    },
    reset() {  // 重置表单
      this.$refs.Form_ref.resetFields()
    }
  }
}
</script>

<style scoped>
.login-box {
  width:100%;
  height: 100%;
  background:rgb(18, 125, 158);
}
.login-content-box {
  width:450px;
  height:300px;
  border-radius: 20px;
  position: relative;
  top:50%;
  left:50%;
  transform:translateX(-50%) translateY(-50%);
  background: rgba(228, 255, 253, 0.534);
  box-sizing: border-box;
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;
}
.el-form {
  width:350px;
}

.login-button-box {
  width:100%;
  display:flex;
  justify-content: center;
}
.el-button {
  margin:20px 45px 0;
}
</style>